import { Messagetype, plugin } from 'alemon'
import { MyDirPath } from '../../app.config.js'
import { join } from 'node:path'
import { existsSync, readFileSync } from 'node:fs'

export class K_毒鸡汤 extends plugin {
  constructor() {
    super({
      /* 说明集*/
      dsc: '毒鸡汤',
      /* 指令集 */
      rule: [
        {
          reg: '^/毒鸡汤$',
          fnc: '毒鸡汤'
        }
      ]
    })
  }
  /**
   * @param e 消息对象
   * @returns
   */
  async 毒鸡汤(e: Messagetype) {
    // https://github.com/able8/nows-nodejs-serverless
    const djtPath = join(MyDirPath, '/resources/data/毒鸡汤.txt')
    if (!existsSync(djtPath)) {
      return e.reply('数据库错误')
    }

    const data = readFileSync(djtPath, 'utf-8')
    const lines = data.replace(/\r/g, '').split('\n')
    const filteredArr = lines.filter(item => item.trim() !== '') // 过滤掉空字符串
    const number = Math.floor(Math.random() * filteredArr.length)
    return e.reply(filteredArr[number])
  }
}
